<?php
//测试控制器类
class IndexController extends CommonController{	
    //动作方法
    public function index(){
    	 
		//nav循环
		
		$nav=$this->nav();
		
		$this->assign('nav',$nav);
		
		//左侧导航循环
		
		$Father=$this->cate();
		
		$this->assign('cate',$Father);
		
		//**************************内容**************************
		
		//随机调出四个商品信息放在banner下面内容
		$count=K("Goods")->count();
		
		//调取缓存
		
	    
		 $s=S('r');
		 
		if(!$s){
			
			$r=mt_rand(1,$count-4);
			S('r',$r,60*60);
			
			$sr=S('r');
			
			$randgoods=K("Goods")->limit("{$sr},4")->all();
			
			 $this->assign('randgoods',$randgoods);
			
		}else{
			
			 $randgoods=K("Goods")->limit("{$s},4")->all();	
			 
             $this->assign('randgoods',$randgoods);
		}
		
//		p($randgoods);
	   

		
		//查找顶级分类
		$main=K("Category")->where("pid=0")->limit('0,6')->all();
		
		
		foreach ($main as $k => $v) {
			
			$main[$k]['cids']=K("Category")->where("pid={$v['cid']}")->getField('cid',true);
				
			$main[$k]['cids']=implode(',', $main[$k]['cids']);		
			
		}
			
		//查找子分类并压入父类
		
		foreach ($main as $k => $v) {
			
			$main[$k]['sons']=K("Category")->where("pid={$v['cid']}")->all();
			
		}

		
		
		
		
		foreach ($main as $k => $v) {
			
			foreach ($v['sons'] as $kk => $vv) {
				
				$main[$k]['goods']=K("Goods")->where("category_cid in ({$v['cids']})")->limit('0,7')->all();
								
			}
			
		}
	
		$this->assign('main',$main);

		
         //显示视图
        $this->display();
    }
}
